// 导入创建路由实例函数和哈希模式函数
import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/Layout/index.vue'
// 路由规则
const routes = [
  {
    path: '/login',
    component: () => import('@/views/Login/index.vue')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '', // 主页
        component: () => import('@/views/Home/index.vue')
      },
      {
        path: 'category/:id', // 分类
        component: () => import('@/views/Category/index.vue')
      },
      {
        path: 'good/:id', // 商品
        component: () => import('@/views/Good/index.vue')
      },
      {
        path: 'cart', // 购物车
        component: () => import('@/views/Cart/index.vue')
      },
      {
        path: 'settlement', // 结算
        component: () => import('@/views/Settlement/index.vue')
      },
      {
        path: 'pay', // 订单支付
        component: () => import('@/views/Pay/index.vue')
      },
      {
        path: 'pay/callback',
        component: () => import('@/views/PayCallback/index.vue')
      }
    ]
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // 路由滚动
  // 该scrollBehavior函数接收to和from路由对象，
  // 该函数可以返回一个ScrollToOptions位置对象：
  scrollBehavior(to, from, savedPosition) {
    // always scroll to top
    return { top: 0 }
  }
})

// 路由跳转后 回归页面顶部
// router.afterEach(() => {
//   window.scroll(0, 0)
// })

export default router
